package com.cz.modules.jk.service.impl;

import com.cz.common.entity.BaseTree;
import com.cz.common.utils.Constant;
import com.cz.modules.sys.entity.SysDeptEntity;
import com.cz.modules.sys.service.SysDeptService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cz.common.utils.PageUtils;
import com.cz.common.utils.Query;

import com.cz.modules.jk.dao.JkCamaraDao;
import com.cz.modules.jk.entity.JkCamaraEntity;
import com.cz.modules.jk.service.JkCamaraService;


@Service("jkCamaraService")
public class JkCamaraServiceImpl extends ServiceImpl<JkCamaraDao, JkCamaraEntity> implements JkCamaraService {
    @Autowired
    private SysDeptService sysDeptService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String cameraName= (String) params.get("cameraName");
        String ip= (String) params.get("ip");
        IPage<JkCamaraEntity> page = this.page(
                new Query<JkCamaraEntity>().getPage(params),
                new QueryWrapper<JkCamaraEntity>()
                        .like(StringUtils.isNotBlank(cameraName),"camera_name", cameraName)
                        .like(StringUtils.isNotBlank(ip),"ip", ip)
                        .apply(params.get(Constant.SQL_FILTER) != null, (String)params.get(Constant.SQL_FILTER))
        );
        List<JkCamaraEntity> list = page.getRecords();
        List<SysDeptEntity> deptlist=sysDeptService.list();
        for(JkCamaraEntity c : list){
            if("00".equals(c.getType())){
                c.setType("收银监控");
            }else if("01".equals(c.getType())){
                c.setType("卡口车流监测");
            }else if("02".equals(c.getType())){
                c.setType("客流监测");
            }else if("03".equals(c.getType())){
                c.setType("安防监控");
            }else if("04".equals(c.getType())){
                c.setType("车位监控");
            }
            for (SysDeptEntity dept:deptlist) {
                if(c.getDeptId() == (dept.getDeptId())){
                    c.setDeptName(dept.getName());
                    break;
                }
            }

        }
        return new PageUtils(page);
    }

    @Override
    public List<BaseTree> camaraTree() {
        List<BaseTree> treelist=new ArrayList<>();
        List<SysDeptEntity> deptlist=sysDeptService.list();
        List<JkCamaraEntity> camaralist=this.list();
        for (SysDeptEntity dept:deptlist) {
            if("2".equals(dept.getParentId().toString())){
                BaseTree tree=new BaseTree();
                tree.setId(dept.getDeptId().toString());
                tree.setName(dept.getName());
                tree.setParentId(dept.getParentId().toString());
                treelist.add(tree);
            }
        }
        for (JkCamaraEntity camara:camaralist) {
            if("00".equals(camara.getType())){
                BaseTree tree=new BaseTree();
                tree.setId(camara.getId());
                tree.setName(camara.getIp());
                tree.setParentId(camara.getDeptId().toString());
                tree.setIsClick(true);
                treelist.add(tree);
            }
        }
        return treelist;
    }

    @Override
    public List<BaseTree> indexsCamaraTree() {
        List<BaseTree> treelist=new ArrayList<>();
        List<SysDeptEntity> deptlist=sysDeptService.list();//所有部门
        List<JkCamaraEntity> camaralist=this.list(//所有监控
                new QueryWrapper<JkCamaraEntity>()
                        .orderByAsc("sort")
        );
        //监控类型 0收银监控 1卡口车流监测 2客流监测 3安防监控 4 车位监控
        List<JkCamaraEntity> camaragrouplist=this.list(//按类型分组后的监控
                new QueryWrapper<JkCamaraEntity>()
                        .groupBy("dept_id","type")
        );
        for (SysDeptEntity dept:deptlist) {
            if("2".equals(dept.getParentId().toString())){
                BaseTree tree=new BaseTree();
                tree.setId(dept.getDeptId().toString());
                tree.setName(dept.getName());
                tree.setParentId(dept.getParentId().toString());
                tree.setIcon("../../statics/css/img/area.png");
                treelist.add(tree);
            }
        }
        for (JkCamaraEntity camaragroup:camaragrouplist) {
            if("01".equals(camaragroup.getType())){
                BaseTree tree=new BaseTree();
                tree.setId(camaragroup.getDeptId()+""+camaragroup.getType());
                tree.setName("室外监控");
                tree.setParentId(camaragroup.getDeptId().toString());
                tree.setIcon("../../statics/css/img/menu.png");
                treelist.add(tree);
            }else if("02".equals(camaragroup.getType())){
                BaseTree tree=new BaseTree();
                tree.setId(camaragroup.getDeptId()+""+camaragroup.getType());
                tree.setName("室内监控");
                tree.setParentId(camaragroup.getDeptId().toString());
                tree.setIcon("../../statics/css/img/menu.png");
                treelist.add(tree);
            }else if("03".equals(camaragroup.getType())){
                BaseTree tree=new BaseTree();
                tree.setId(camaragroup.getDeptId()+""+camaragroup.getType());
                tree.setName("客流监控");
                tree.setParentId(camaragroup.getDeptId().toString());
                tree.setIcon("../../statics/css/img/menu.png");
                treelist.add(tree);
            }else if("04".equals(camaragroup.getType())){
                BaseTree tree=new BaseTree();
                tree.setId(camaragroup.getDeptId()+""+camaragroup.getType());
                tree.setName("收银稽查");
                tree.setParentId(camaragroup.getDeptId().toString());
                tree.setIcon("../../statics/css/img/menu.png");
                treelist.add(tree);
            }else if("05".equals(camaragroup.getType())){
                BaseTree tree=new BaseTree();
                tree.setId(camaragroup.getDeptId()+""+camaragroup.getType());
                tree.setName("卡口监控");
                tree.setParentId(camaragroup.getDeptId().toString());
                tree.setIcon("../../statics/css/img/menu.png");
                treelist.add(tree);
            }
        }
        for (JkCamaraEntity ca:camaralist) {
            if(ca.getCameraIndexCode()!=null && !"".equals(ca.getCameraIndexCode())){
                BaseTree tree=new BaseTree();
                tree.setId(ca.getId());
                tree.setName(ca.getCameraName());
                tree.setParentId(ca.getDeptId()+""+ca.getType());
                tree.setData(ca.getCameraIndexCode());
                tree.setIcon("../../statics/css/img/camera.png");
                tree.setIsClick(true);
                treelist.add(tree);
            }
        }
        return treelist;
    }
    @Override
    public List<BaseTree> indexsCamaraTree1() {
        List<BaseTree> treelist=new ArrayList<>();
        List<SysDeptEntity> deptlist=sysDeptService.list();//所有部门
        List<JkCamaraEntity> camaralist=this.list(//所有监控
                new QueryWrapper<JkCamaraEntity>()
                        .orderByAsc("sort")
        );
        //监控类型 0收银监控 1卡口车流监测 2客流监测 3安防监控 4 车位监控
        List<JkCamaraEntity> camaragrouplist=this.list(//按类型分组后的监控
                new QueryWrapper<JkCamaraEntity>()
                        .groupBy("dept_id","type")
        );
        for (SysDeptEntity dept:deptlist) {
            if("2".equals(dept.getParentId().toString())){
                BaseTree tree=new BaseTree();
                tree.setId(dept.getDeptId().toString());
                tree.setName(dept.getName());
                tree.setParentId(dept.getParentId().toString());
                tree.setIcon("../../statics/css/img/area1.png");
                treelist.add(tree);
            }
        }
        for (JkCamaraEntity camaragroup:camaragrouplist) {
            if("00".equals(camaragroup.getType())){
                BaseTree tree=new BaseTree();
                tree.setId(camaragroup.getDeptId()+""+camaragroup.getType());
                tree.setName("收银监控");
                tree.setParentId(camaragroup.getDeptId().toString());
                tree.setIcon("../../statics/css/img/menu1.png");
                treelist.add(tree);
            }else if("01".equals(camaragroup.getType())){
                BaseTree tree=new BaseTree();
                tree.setId(camaragroup.getDeptId()+""+camaragroup.getType());
                tree.setName("卡口车流监控");
                tree.setParentId(camaragroup.getDeptId().toString());
                tree.setIcon("../../statics/css/img/menu1.png");
                treelist.add(tree);
            }else if("02".equals(camaragroup.getType())){
                BaseTree tree=new BaseTree();
                tree.setId(camaragroup.getDeptId()+""+camaragroup.getType());
                tree.setName("客流监控");
                tree.setParentId(camaragroup.getDeptId().toString());
                tree.setIcon("../../statics/css/img/menu1.png");
                treelist.add(tree);
            }else if("03".equals(camaragroup.getType())){
                BaseTree tree=new BaseTree();
                tree.setId(camaragroup.getDeptId()+""+camaragroup.getType());
                tree.setName("安防监控");
                tree.setParentId(camaragroup.getDeptId().toString());
                tree.setIcon("../../statics/css/img/menu1.png");
                treelist.add(tree);
            }else if("04".equals(camaragroup.getType())){
                BaseTree tree=new BaseTree();
                tree.setId(camaragroup.getDeptId()+""+camaragroup.getType());
                tree.setName("车位监控");
                tree.setParentId(camaragroup.getDeptId().toString());
                tree.setIcon("../../statics/css/img/menu1.png");
                treelist.add(tree);
            }else if("05".equals(camaragroup.getType())){
                BaseTree tree=new BaseTree();
                tree.setId(camaragroup.getDeptId()+""+camaragroup.getType());
                tree.setName("卡口监控");
                tree.setParentId(camaragroup.getDeptId().toString());
                tree.setIcon("../../statics/css/img/menu1.png");
                treelist.add(tree);
            }
        }
        for (JkCamaraEntity ca:camaralist) {
            if(ca.getCameraIndexCode()!=null && !"".equals(ca.getCameraIndexCode())){
                BaseTree tree=new BaseTree();
                tree.setId(ca.getId());
                tree.setName(ca.getCameraName());
                tree.setParentId(ca.getDeptId()+""+ca.getType());
                tree.setData(ca.getCameraIndexCode());
                tree.setIcon("../../statics/css/img/camera1.png");
                tree.setIsClick(true);
                treelist.add(tree);
            }
        }
        return treelist;
    }

    @Override
    public List<BaseTree> northSouthCamara(String deptId) {
        List<BaseTree> treelist=new ArrayList<>();
        List<SysDeptEntity> deptlist=sysDeptService.list();//所有部门
        List<JkCamaraEntity> camaralist=this.list(//该部门所有监控
                new QueryWrapper<JkCamaraEntity>()
                        .eq("dept_id",deptId)
                        .orderByAsc("sort")
        );
        for (SysDeptEntity dept:deptlist) {
            if(deptId.equals(dept.getDeptId().toString())){
                BaseTree tree=new BaseTree();
                tree.setId(dept.getDeptId().toString());
                tree.setName(dept.getName());
                tree.setParentId("0");
                treelist.add(tree);
            }
        }
        //监控类型 01室外 02室内 03客流监测 04收银稽查
        List<JkCamaraEntity> camaragrouplist=this.list(//按类型分组后的监控
                new QueryWrapper<JkCamaraEntity>()
                        .eq("dept_id",deptId)
                        .groupBy("type")
        );
        for (JkCamaraEntity camaragroup:camaragrouplist) {
            if("01".equals(camaragroup.getType())){
                BaseTree tree=new BaseTree();
                tree.setId(camaragroup.getDeptId()+""+camaragroup.getType());
                tree.setName("室外监控");
                tree.setParentId(camaragroup.getDeptId().toString());
                treelist.add(tree);
            }else if("02".equals(camaragroup.getType())){
                BaseTree tree=new BaseTree();
                tree.setId(camaragroup.getDeptId()+""+camaragroup.getType());
                tree.setName("室内监控");
                tree.setParentId(camaragroup.getDeptId().toString());
                treelist.add(tree);
            }else if("03".equals(camaragroup.getType())){
                BaseTree tree=new BaseTree();
                tree.setId(camaragroup.getDeptId()+""+camaragroup.getType());
                tree.setName("客流监控");
                tree.setParentId(camaragroup.getDeptId().toString());
                treelist.add(tree);
            }else if("04".equals(camaragroup.getType())){
                BaseTree tree=new BaseTree();
                tree.setId(camaragroup.getDeptId()+""+camaragroup.getType());
                tree.setName("收银稽查");
                tree.setParentId(camaragroup.getDeptId().toString());
                treelist.add(tree);
            }else if("05".equals(camaragroup.getType())){
                BaseTree tree=new BaseTree();
                tree.setId(camaragroup.getDeptId()+""+camaragroup.getType());
                tree.setName("卡口监控");
                tree.setParentId(camaragroup.getDeptId().toString());
                treelist.add(tree);
            }
        }
        for (JkCamaraEntity ca:camaralist) {
            if(ca.getCameraIndexCode()!=null && !"".equals(ca.getCameraIndexCode())){
                BaseTree tree=new BaseTree();
                tree.setId(ca.getId());
                tree.setName(ca.getCameraName());
                tree.setParentId(ca.getDeptId()+""+ca.getType());
                tree.setData(ca.getCameraIndexCode());
                tree.setIsClick(true);
                treelist.add(tree);
            }
        }
        return treelist;
    }

}
